$(function () {
    //供货信息搜索接口  2.1.4  转跳页面
    var getUrl = "/home/searchProvideInfos";

//请求数据
    var urlAddress = window.location.href;
    var dataType = '',
        meatTypes = getQueryString('meatType');
    if(urlAddress.indexOf("?")!= -1){
        var urlArr = urlAddress.split('?');
        dataType = urlArr[1];
        //取meatType 的值
    }

    var $window = $(window),
        viewHeight = $window.height(),
        contentHeight = $('#meal-info ul').height(),
        loadNum = 0,
        numScroll = 1,
        loadTotal = 0;
    loadTotalSurplus = 10,
        time = '',
        keyWord = '',
        country = '';
    meatType = getQueryString('meatType');
//	var str =
    if(keyWord == null){
        keyWord = '';
    }
    if(country == null){
        country = '';
    }
    if(time == null){
        time = 30;
    }

    if(meatType == 0){
        $('#side_delivery_nav li').eq(1).text('牛肉')

    }else{

        $('#side_delivery_nav li').eq(1).text('羊肉')

    }

    $('#side_delivery_nav li').on('click',function(){
        if($(this).index() == 0){
            //返回首页
            window.location.href = 'index.html';

        }
    })


    var  setmeatType = function (obj) {
            dataTypes = window.location.search.substring(1)
            return dataTypes;
        },
        requestData = function (obj,fun) {
            $.ajax({
                url: host + getUrl,
                type: 'post',
                data: dataTypes,
                success: function (data) {
                    fun(data)
                }
            });
        },
        filePage = function () {
            var indexData = JSON.parse(window.sessionStorage.getItem('indexSearchData'));
            window.sessionStorage.removeItem('indexSearchData')
            window.sessionStorage.removeItem('indexSearchHistory')
            meatTypes = indexData.meatTypes;
            if(Number(meatTypes) !== 0){
                $('#nav_delivery ol li').eq(0).text('羊肉')
                $('#nav_delivery ol li').eq(1).text('牛肉')
            }
            jrollinit.scroller.innerHTML = indexData.data;
            jrollinit.options.page = indexData.page;
            // if(indexData.current >= 5){
            //     requestData(setmeatType({
            //         meatTypes: meatTypes,
            //         offset: jrollinit.options.page
            //     }),function (data) {
            //         if (data.success == true) {
            //             jrollinit.options.page += 1
            //             jrollinit.infinite_callback(data.data)
            //             jrollinit.refresh();
            //             jrollinit.scrollTo(0, indexData.top, 0, false, function() {
            //                 //滑动结束后执行
            //                 lay.msg('已返回到之前浏览的位置')
            //             });
            //         } else {
            //             //无数据则返回什么
            //             console.log(data)
            //         }
            //     });
            // }else {
            jrollinit.refresh();
            jrollinit.scrollTo(0, indexData.top, 0, false, function() {
                //滑动结束后执行
                lay.msg('已返回到之前浏览的位置')
            });
            // }

        };


// 默认首屏渲染
    jroll();
    window.fn = function (Ele) {
        var obj = {
            page:jrollinit.options.page,
            data:jrollinit.scroller.innerHTML,
            top:jrollinit.y - $(Ele).offset().top + 50 +12,
            meatTypes:meatTypes,
            current:$(Ele).index(),
            search:window.location.search
        };
        window.sessionStorage.setItem('indexSearchData', JSON.stringify(obj))
    }
    function jroll() {
        jrollinit = new JRoll("#meal-info", {
            scrollBarY: true,
            scrollBarFade: true
        });   // jroll 对象
        // $('#meal-info').css('overflow','visible')

        // 下拉刷新
        jrollinit.pulldown({
            refresh: function(complete) {
                jrollinit.options.page = 1;
                jrollinit.scrollTo(0, 44, 0, true);  //滚回顶部
                requestData(setmeatType({
                    meatTypes: meatTypes,
                    offset: jrollinit.options.page - 1
                }),function (data) {
                    if (data.success == true) {
                        complete();
                        jrollinit.scroller.innerHTML = "";    //清空内容
                        jrollinit.infinite_callback(data.data);
                    } else {
                        //无数据则返回什么
                        console.log(data)
                    }
                });
            }
        });
        // 无限加载

        jrollinit.infinite({
            options: jrollinit,
            completeTip: '<div class=\"jroll-infinite-tip\" style=\"text-align\: center\;\">已经加载完毕全部数据</div>',
            loadingTip: '<div class=\"jroll-infinite-tip\" style=\"text-align\: center\;\">正在加载...</div>',
            template: '<li onclick="fn(this)" ><a  href="deliveryDetails.html?indexSearchHistory=true&id={{=_obj.id}}"><div class="row"><div class="col-xs-4" id="photo"><img src={{=_obj.imageList[0]?_obj.imageList[0]:"img\/defaultImg.png"}}></div><div class="col-xs-8 "><p>{{=_obj.meatCategory[0]?_obj.meatCategory[0]:"暂无数据"}}</p><p id="price"><span><em>¥{{=_obj.price?_obj.price+ "/吨":"议定"}}</em></span></p><p><span>产地：<em>{{=_obj.country}}</em></span><span id="margin">所在地：{{=_obj.warehouse}}</em></span></p><p><span>数量：<em>{{=Math.round(_obj.amount / 1000 * 100) / 100}}吨</em></span>  <span>供货类型：<em>{{=_obj.provideWay}}</em></p></div></div></a></li>',
            // template: '<li _dataNum ="{{=_obj.id}}"><h3>{{=_obj.meatCategory[0]?_obj.meatCategory[0]:"暂无数据"}}</h3><p><em>¥{{=_obj.price?_obj.price+ "/吨":"议定"}}</em></p><ol class="clear mian_delivery_ol"><li>数量：<span>{{=Math.round(_obj.amount / 1000 * 100) / 100}}</span>吨</li><li>交货地：<span>{{=_obj.deliveryArea}}</span></li><li class="mian_delivery_ol_add"><button class="{{=_obj.class = _obj.status === 3?\"colorName\":\"\"}}">{{=_obj.status===3?\"供应中\":\"供应\"}}</button></li></ol></li>',
            getData: function (page, callback) {
                var history = window.sessionStorage.getItem('indexSearchHistory');
                if(history){
                    filePage();
                }else {
                    requestData(setmeatType({
                        meatTypes: meatTypes,
                        offset: page - 1
                    }),function (data) {
                        if (data.success == true) {
                            jrollinit.options.total = Math.ceil(data.total / 10);
                            if(page >= jrollinit.options.total){
                                jrollinit.options.page = jrollinit.options.total;
                            }
                            callback(data.data)
                        } else {
                            //无数据则返回什么
                            console.log(data)
                        }
                    });
                }
            },
        });
        // jrollinit.on("scrollEnd", function () {
        //     if (pageI > jrollinit.options.total) {
        //         $('.jroll-infinite-tip').html('已经加载完毕全部数据')
        //     }
        // });
        jrollinit.refresh();
        return jrollinit;
    }


})